package com.dudsbury.database;

import java.util.ArrayList;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import com.dudsbury.gui.BookingFrame;
import com.dudsbury.gui.SearchScreen;

public class Booking implements Searchable {

	private DateTime date;
	private boolean bookingDay, bookingNight;
	private DateTime bookingDate;
	private int occupancyAdult, occupancyChild;
	private String deposit, itemTotal, otherExpenses, vat, total, customerid,
			startTime, finishTime;
	private String bookingType, bookingName, details, room, bookingId;
	private MyRoom[] rooms;
	private String[] data;
	private String cusName;
	private static final int BOOKING_ID = 0;
	private static final int BOOKING_TYPE = 1;

	private static final int BOOKING_DATE = 2;
	private static final int BOOKING_DAY = 3;
	private static final int BOOKING_NIGHT = 4;
	private static final int START_TIME = 5;
	private static final int FINISH_TIME = 6;
	private static final int ADULT_OCC = 7;
	private static final int CHILD_OCC = 8;
	private static final int DEPOSIT = 9;
	private static final int ITEM_TOTAL = 10;
	private static final int VAT = 11;
	private static final int TOTAL = 12;
	private static final int DETAILS = 13;

	private static final int CUSTOMER = 14;

	public Booking() {

	}

	public Booking(String data[]) {
		this.data = data;
		this.bookingId = data[BOOKING_ID];
		this.bookingType = data[BOOKING_TYPE];

		String hold[] = data[BOOKING_DATE].split("-");
		// System.out.println("Hold Length: " + hold.length);
		try {
			this.bookingDate = new DateTime(Integer.parseInt(hold[0]),
					Integer.parseInt(hold[1]), Integer.parseInt(hold[2]), 0, 0);
			// System.out.println("Working String " + data[BOOKING_DATE]);
		} catch (Exception e) {
			System.out.println("Error " + e);
			// System.out.println(" Not Working String " + data[BOOKING_DATE]);
		}
		if (data[BOOKING_DAY].equals("1"))
			this.bookingDay = true;
		if (data[BOOKING_NIGHT].equals("1"))
			this.bookingNight = true;

		startTime = data[START_TIME];

		finishTime = data[FINISH_TIME];
		occupancyAdult = Integer.valueOf(data[ADULT_OCC]);
		try {
			occupancyChild = Integer.valueOf(data[CHILD_OCC]);
		} catch (Exception e) {
			occupancyChild = 0;
		}
		deposit = data[DEPOSIT];
		itemTotal = data[ITEM_TOTAL];
		vat = data[VAT];
		total = data[TOTAL];
		details = data[DETAILS];
		customerid = data[CUSTOMER].replaceAll("\n", "").trim();
		
		cusName = "";
		for (MyCustomer c : DatabaseExtension.getCustomers()) {
		if (c.getCustomerID().trim().replaceAll("\n", "").equals(customerid)) {
			cusName = String.format("%s %s", c.getForename(),c.getSurname());
		}
		}
	}

	public String getCustomerid() {
		return customerid;
	}

	public void setCustomerid(String customerid) {
		this.customerid = customerid;
	}

	public String getRoom() {
		return room;
	}

	public void setRoom(String room) {
		this.room = room;
	}

	/*
	 * public MyBooking(MyDateTime date) { this.date=date; }
	 */
	public String getBookingId() {
		return bookingId.replaceAll("\n", "").trim();
	}

	public void setBookingId(String bookingId) {
		this.bookingId = bookingId;
	}

	public String getBookingType() {
		return bookingType;
	}

	public void setBookingType(String bookingType) {
		this.bookingType = bookingType;
	}

	public String getBookingName() {
		return bookingName;
	}

	public void setBookingName(String bookingName) {
		this.bookingName = bookingName;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public DateTime getDate() {
		return bookingDate;
	}

	public String getDateAsString() {
		try {
			return getDate().toString(DateTimeFormat.forPattern("y-M-d"));
		} catch (Exception e) {
			return null;
		}
	}

	public void setDate(DateTime date) {
		this.bookingDate = date;
	}

	public boolean isBookingDay() {
		return bookingDay;
	}

	public void setBookingDay(boolean bookingDay) {
		this.bookingDay = bookingDay;
	}

	public boolean isBookingNight() {
		return bookingNight;
	}

	public void setBookingNight(boolean bookingNight) {
		this.bookingNight = bookingNight;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getFinishTime() {
		return finishTime;
	}

	public void setFinishTime(String finishTime) {
		this.finishTime = finishTime;
	}

	public int getOccupancyAdult() {
		return occupancyAdult;
	}

	public void setOccupancyAdult(int occupancyAdult) {
		this.occupancyAdult = occupancyAdult;
	}

	public int getOccupancyChild() {
		return occupancyChild;
	}

	public void setOccupancyChild(int occupancyChild) {
		this.occupancyChild = occupancyChild;
	}

	public String getDeposit() {
		return deposit;
	}

	public void setDeposit(String deposit) {
		this.deposit = deposit;
	}

	public String getOtherExpenses() {
		return otherExpenses;
	}

	public void setOtherExpenses(String otherExpenses) {
		this.otherExpenses = otherExpenses;
	}

	@Override
	public String toString() {
		
		return String.format("<html><b>%s</b></html>", cusName);
		// return String.format("name: %s Date: %s", custname,custdate);
	}

	public DateTime getBookingDate() {
		return bookingDate;
	}

	public void setBookingDate(DateTime bookingDate) {
		this.bookingDate = bookingDate;
	}

	public String getItemTotal() {
		return itemTotal;
	}

	public void setItemTotal(String itemTotal) {
		this.itemTotal = itemTotal;
	}

	public String getVat() {
		return vat;
	}

	public void setVat(String vat) {
		this.vat = vat;
	}

	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	public int getDay() {
		if (bookingDay)
			return 1;
		else
			return 0;

	}

	public int getNight() {
		if (bookingNight)
			return 1;
		else
			return 0;

	}

	@Override
	public void onSelected(SearchScreen screen) {
		new BookingFrame(this,DatabaseExtension.getRoomsByBooking(bookingId)[0],screen);

	}

	@Override
	public boolean search(String[] query) {

		
		
		for (String s : query) {
			
					ArrayList<String> list = new ArrayList<String>();
					for (String h :this.getDateAsString().split("-")) {
						list.add(h);
					}
					 for (String t :  details.split(" ")) {
					list.add(t);
					 }
					for (MyCustomer c :  DatabaseExtension.getCustomersByBooking(bookingId)) {
						 list.add(c.getForename());
						 list.add(c.getSurname());
						 list.add(c.getAddress1());
						 list.add(c.getAddress2());
					 }
					for (String d : list) {
					if (d.equals(s)) {
						System.out.println("Found Result in Booking");
						return true;
					}
				}
			}
		
		return false;
	}
	
	public String getReport() {
		StringBuilder report = new StringBuilder();
		
		report.append(String.format("Date: %d/%d/%d~", bookingDate.getDayOfMonth(),bookingDate.getMonthOfYear(),bookingDate.getYear()));
		report.append(String.format("Customer: %s~" +
				" ~ Start Time: %s ~ Finish Time: %s ~ Details: %s ~ ",cusName,
				startTime,finishTime,details));
		
		
		MyItem items[] = DatabaseExtension.getItemsByBooking(bookingId.replaceAll("\n","").trim());
		
		report.append("Items: ~");
		for (MyItem i : items) {
			
			report.append(String.format("%s Quantity: %d~", i.getItemName(),DatabaseExtension.getItemQuantity(i.getItemID(),bookingId)));
			
		}
		return report.toString();
	}
/*
 * private DateTime date;
	private boolean bookingDay, bookingNight;
	private DateTime bookingDate;
	private int occupancyAdult, occupancyChild;
	private String deposit, itemTotal, otherExpenses, vat, total, customerid,
			startTime, finishTime;
	private String bookingType, bookingName, details, room, bookingId;
 */
}
